package com.ax.api.route.business.common;

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.ax.api.route.util.RouteConfig;
@RestController
@RequestMapping("/bucommon")
public class BuCommonRoute {
    @Autowired
    LoadBalancerClient loadBalancerClient;
    @Autowired
    RestTemplate restTemplate;
	    
    /**
	 * getMakeOrder
	 * remote_bussiness
	 * 获取订单号
	 */   
	@GetMapping("/getMakeOrder")
	public String getMakeOrder(@RequestParam Map<String,Object> map,HttpServletRequest request){
		Object sessionId = request.getSession().getAttribute("sessionId");
		map.put("sessionId", sessionId);
		String value = "?businessCode={businessCode}&sessionId={sessionId}";
		String url = geturl("/getMakeOrder")+value;
		return restTemplate.getForObject(url, String.class,map);
  }
	
	   private String geturl(String path){
		 ServiceInstance serviceInstance = loadBalancerClient.choose(RouteConfig.BUSSINESS_ROUTE);
	//	 String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort()+path;
		 StringBuffer bf = new StringBuffer("http://");
		 bf.append(serviceInstance.getHost());
		 bf.append(":");
		 bf.append(serviceInstance.getPort());
		 bf.append(path);
		 return bf.toString();
		   
	   }
   /**
	 * insertAssociator
	 * remote_bussiness
	 * 新增会员(第一种方式)
	 */   
		@GetMapping("/insertAssociator")
		public String insertAssociator(@RequestParam Map<String,Object> map){
		String value = "?associatorName={associatorName}&associatorName={associatorName}&personId={personId}&sex={sex}&address={address}&remarks={remarks}&drugstoreId={drugstoreId}&drugstoreName={drugstoreName}&registrarId={registrarId}&registrarName={registrarName}&registrarId={registrarId}&registrarName={registrarName}&flag={flag}";
		String url = geturl("/insertAssociator")+value;    
	    return restTemplate.getForObject(url, String.class,map);
	  }
		
	/**
	 * queryAssociator
	 * remote_bussiness
	 * 查询会员
	 */   
		
		@GetMapping("/queryAssociator")
		public String queryAssociator(@RequestParam Map<String,Object> map){
		String value = "?associatorName={associatorName}&associatorName={associatorName}&personId={personId}&sex={sex}&address={address}&remarks={remarks}&drugstoreId={drugstoreId}&drugstoreName={drugstoreName}&registrarId={registrarId}&registrarName={registrarName}&registrarId={registrarId}&registrarName={registrarName}&flag={flag}&offset={offset}&limit={limit}";
		String url = geturl("/queryAssociator")+value;    
	    return restTemplate.getForObject(url, String.class,map);
	  }
	/**
	 * updateAssociator
	 * remote_bussiness
	 * 更新会员
	 */
		@GetMapping("/updateAssociator")
		public String updateAssociator(@RequestParam Map<String,Object> map){
		String value = "?associatorName={associatorName}&associatorName={associatorName}&personId={personId}&sex={sex}&address={address}&remarks={remarks}&drugstoreId={drugstoreId}&drugstoreName={drugstoreName}&registrarId={registrarId}&registrarName={registrarName}&registrarId={registrarId}&registrarName={registrarName}&flag={flag}&offset={offset}&limit={limit}";
		String url = geturl("/updateAssociator")+value;    
	    return restTemplate.getForObject(url, String.class,map);
	  }
		
	/**
	 * queryBaseDoctor
	 * remote_bussiness
	 * 查看医生详情
	 */
		@GetMapping("/queryBaseDoctor")
		public String queryBaseDoctor(@RequestParam Map<String,Object> map){
		String value = "?doctorId={doctorId}&subjectId={subjectId}&offset={offset}&limit={limit}";
		String url = geturl("/queryBaseDoctor")+value;    
	    return restTemplate.getForObject(url, String.class,map);
	  }
		
	/**
	 * queryBasePharmacist
	 * remote_bussiness
	 * 查看药师详情
	 */
		@GetMapping("/queryBasePharmacist")
		public String queryBasePharmacist(@RequestParam Map<String,Object> map){
		String value = "?pharmacistId={pharmacistId}&offset={offset}&limit={limit}";
		String url = geturl("/queryBasePharmacist")+value;    
	    return restTemplate.getForObject(url, String.class,map);
	  }
}
